/*
 * @Author: 杨三炮
 * @Date: 2021-11-18 19:05:32
 * @LastEditors: 杨三炮
 * @LastEditTime: 2021-11-23 20:17:51
 * @FilePath: \node\node\app.js
 */
const express = require('express')
const app = express()
const port = 3000
const bodyParser = require('body-parser')
const path = require('path')
const passport = require('passport')
global.globalKey = '123456'

/**
 * 全系统允许跨域处理 这段配置要再new出express实例的时候就要设置了，放在所有的api前面，不然没有效果
*/
app.all("*", function (req, res, next) {
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin", "*");
    //允许的header类型
    res.header("Access-Control-Allow-Headers", "*");
    //跨域允许的请求方式
    res.header("Access-Control-Allow-Methods", "*");
    if (req.method.toLowerCase() == 'options')
        res.send(200);  //让options尝试请求快速结束
    else
        next();
});
//设置默认目录
app.use(express.static(path.join(__dirname, 'public')))
//使用bodyoarser中间件获取数据
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

//passport初始化
app.use(passport.initialize())
require('./config/passport')(passport)

//默认进入首页
app.use('/', require('./routers/index'))
//进入用户路由
app.use('/users', require('./routers/users'))
//进入管理员路由
app.use('/admin', require('./routers/admin'))


app.use('/student', require('./routers/student'))

app.use('/teacher', require('./routers/teacher'))


app.listen(port, () => console.log(`http://127.0.0.1:${port}`))